define(['app', 'ace-language-tools'], function(app) {
    app.controller('MockGitFileController', function($scope, $compile, $http, xUtil, xContext, $uibModalInstance) {
        var self = this;
        self.sha = '';
        self.fullScreen = false;

        var dataViewer = null;

        self.init = function() {
            self.sha = xContext.sha;
            // Data viewer
            var modeId = 'ace/mode/text';
            if (xContext.path.lastIndexOf('.json') > 0) {
                modeId = 'ace/mode/hjson';
            } else if (xContext.path.lastIndexOf('.xml') > 0) {
                modeId = 'ace/mode/xml';
            } else if (xContext.path.lastIndexOf('.md') > 0) {
                modeId = 'ace/mode/markdown';
            }
            dataViewer = ace.edit('data-viewer', {
                mode: modeId,
                theme: 'ace/theme/chrome',
                fontSize: 14,
                readOnly: true,
                useWorker: false,
                showPrintMargin: false,
                highlightGutterLine: false,
                highlightActiveLine: false,
                autoScrollEditorIntoView: true
            });

            xUtil.clipboard.copy('#copy-data', function() {
                return dataViewer.getValue();
            });

            $('#data-viewer .ace_scroller').append(
                    $compile($('#viewer-tools').html())($scope)
                );
            $('#data-viewer .x-editor-tool').mousedown(function(e) { return false; });

            $scope.$watch('dvm.fullScreen', function(fullScreen) {
                if (fullScreen) {
                    $('.modal-dialog:eq(0)').toggleClass('x-modal-max');
                } else {
                    $('.modal-dialog:eq(0)').removeClass('x-modal-max');
                }
                setTimeout(function() {
                    dataViewer.resize();
                }, 50);
            });

            $http({
                method: 'POST',
                url: '/mock/git/file',
                data: { file: xContext.path, sha: self.sha },
                transformResponse: function(response) {
                    return response;
                }
            }).then(function(response) {
                if (response.status == 200) {
                    dataViewer.setValue(response.data, -1);
                    dataViewer.resize();
                }
            });
        }

        self.close = function() {
            $uibModalInstance.close();
        }
    });
});
